﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace CommonLibrary.CustControls
{
    public enum BoolEnum
    {
        True,
        False
    };

    class CusToolboxItem
    {
        private String controlName = null;
        private String controlTitle = null;
        private String controlTypeName = null;
        private Type controlType = null;
        private Image controlImg = null;
        public static List<String> ControlNames = new List<string>();

        private BoolEnum fillable = BoolEnum.True;

        [DisplayName("可填写")]
        public BoolEnum Fillable
        {
            get { return fillable; }
            set { fillable = value; }
        }

        public CusToolboxItem(String controlName, String controlTitle, String controlTypeName)
        {
            this.controlName = controlName;
            this.controlTitle = controlTitle;
            this.controlTypeName = controlTypeName;
        }

        public CusToolboxItem(String controlName, String controlTitle, String controlTypeName,Boolean fillable)
        {
            this.fillable = fillable == true ? BoolEnum.True : BoolEnum.False;
            this.controlName = controlName;
            this.controlTitle = controlTitle;
            this.controlTypeName = controlTypeName;
        }

        [DisplayName("新建列类型")]
        public Type ControlType
        {
            get
            {
                if (controlType == null)
                    controlType = Type.GetType(controlTypeName);
                if (controlType == null)
                {
                    Assembly assembly = Assembly.GetAssembly(typeof(DataGridViewColumn));
                    controlType = assembly.GetType(controlTypeName);
                }
                return controlType;
            }
        }

        [Browsable(false)]
        public Image ControlImg
        {
            get
            {
                if (controlImg == null)
                {
                    Type t = ControlType;
                    if (t == null)
                        t = typeof(Component);
                    ToolboxBitmapAttribute attr = (ToolboxBitmapAttribute)
                        TypeDescriptor.GetAttributes(t)[typeof(ToolboxBitmapAttribute)];
                    if (attr != null)
                        controlImg = attr.GetImage(t, false);
                }
                return controlImg;
            }
        }

        [DisplayName("列名")]
        public String ControlName
        {
            get
            {
                return controlName;
            }
            set
            {
                if (ValidateColName(value))
                    controlName = value;
                else
                    MessageBox.Show("其他列已经在使用名称 " + value, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        [DisplayName("页眉文字")]
        public String ControlTitle
        {
            get
            {
                return controlTitle;
            }
            set
            {
                controlTitle = value;
            }
        }

        /// <summary>
        ///  列名验证
        /// </summary>
        /// <param name="colName"></param>
        /// <returns></returns>
        private Boolean ValidateColName(String colName)
        {
            foreach (var item in ControlNames)
            {
                if (item == colName)
                    return false;
            }
            return true;
        }
    }
}
